package com.campus.bbs;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import com.campus.utils.Http;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.ListView;

public class DetailController {
	ListView listview;
	String url;
	String resp;
	DetailAdapter adapter;
	ArrayList<DetailInfo> list;

	View getView() {
		return listview;
	}

	public DetailController(Context context, String url) {
		listview = new ListView(context);
		this.url = url;
		list = new ArrayList<DetailInfo>();
		adapter = new DetailAdapter(context, list);
		listview.setAdapter(adapter);
		go();
	}

	void go() {
		new Thread() {
			public void run() {

				try {
					resp = Http.get(url, "UTF-8");
					Handler handler = new Handler(Looper.getMainLooper());
					handler.post(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							list.addAll(DetailHelper.resp(resp));
							adapter.notifyDataSetChanged();
						}
					});
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}.start();
	}

}
